/*
 * Copyright (C) 2012-2016 TeraCN
 * 
 * This file is part of TeraCN.
 * 
 * TeraCN is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * TeraCN is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package tera.gameserver.model.quests.events;

import java.util.Arrays;

import org.w3c.dom.Node;

import rlib.logging.Logger;
import rlib.logging.Loggers;

import tera.gameserver.model.quests.Quest;
import tera.gameserver.model.quests.QuestAction;
import tera.gameserver.model.quests.QuestEvent;
import tera.gameserver.model.quests.QuestEventListener;
import tera.gameserver.model.quests.QuestEventType;

/**
 * @author Ronn Mod Evestu
 */
public abstract class AbstractQuestEventListener implements QuestEventListener
{
	protected static final Logger log = Loggers.getLogger(AbstractQuestEventListener.class);
	protected QuestEventType type;
	protected Quest quest;
	protected QuestAction[] actions;
	
	/**
	 * Constructor for AbstractQuestEventListener.
	 * @param type QuestEventType
	 * @param actions QuestAction[]
	 * @param quest Quest
	 * @param node Node
	 */
	public AbstractQuestEventListener(QuestEventType type, QuestAction[] actions, Quest quest, Node node)
	{
		try
		{
			this.type = type;
			this.actions = actions;
			this.quest = quest;
		}
		catch (Exception e)
		{
			log.warning(this, e);
		}
	}
	
	/**
	 * Method getActions.
	 * @return QuestAction[]
	 */
	protected final QuestAction[] getActions()
	{
		return actions;
	}
	
	/**
	 * Method getType.
	 * @return QuestEventType
	 * @see tera.gameserver.model.quests.QuestEventListener#getType()
	 */
	@Override
	public final QuestEventType getType()
	{
		return type;
	}
	
	/**
	 * Method notifyQuest.
	 * @param event QuestEvent
	 * @see tera.gameserver.model.quests.QuestEventListener#notifyQuest(QuestEvent)
	 */
	@Override
	public void notifyQuest(QuestEvent event)
	{
		final QuestAction[] actions = getActions();
		
		for (QuestAction action : actions)
		{
			if (action.test(event.getNpc(), event.getPlayer()))
			{
				action.apply(event);
			}
		}
	}
	
	/**
	 * Method toString.
	 * @return String
	 */
	@Override
	public String toString()
	{
		return "AbstractQuestEventListener type = " + type + ", quest = " + quest.getId() + ", actions = " + Arrays.toString(actions);
	}
}